package com.essence.grabiec104.grab.config;

import org.flywaydb.core.Flyway;
import org.flywaydb.core.api.FlywayException;
import org.flywaydb.core.api.configuration.ClassicConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;

import javax.annotation.PostConstruct;
import javax.sql.DataSource;

@Order(1)
@Configuration
public class FlywayConfig {
    @Autowired
    private DataSource dataSource;

    @PostConstruct
    public void init() {
        ClassicConfiguration conf = new ClassicConfiguration();
        conf.setDataSource(dataSource);
        conf.setOutOfOrder(true);
        Flyway flyway = new Flyway(conf);
        try {
            flyway.migrate();
        } catch (FlywayException e) {
            e.printStackTrace();
        }
    }

}
